package com.snowfeel.sfmall.sys.service;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snowfeel.sfmall.core.param.IdParam;
import com.snowfeel.sfmall.core.param.IdsParam;
import com.snowfeel.sfmall.core.vo.PageVo;
import com.snowfeel.sfmall.sys.convert.SysDictItemConvert;
import com.snowfeel.sfmall.sys.dto.SysDictItemParam;
import com.snowfeel.sfmall.sys.mapper.SysDictItemMapper;
import com.snowfeel.sfmall.sys.model.SysDictItem;
import com.snowfeel.sfmall.sys.query.SysDictItemQuery;
import com.snowfeel.sfmall.sys.vo.SysDictItemVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 数据字典项业务实现类
 *
 * @author haoxr
 * @date 2022/10/12
 */
@Service
public class SysDictItemService extends ServiceImpl<SysDictItemMapper, SysDictItem> {

    @Autowired
    private SysDictItemConvert dictItemConvert;

    /**
     * 字典数据项分页列表
     *
     * @param query
     * @return
     */
    public PageVo<SysDictItemVo> page(SysDictItemQuery query) {
        // 查询参数
        long pageNum = query.getPageNo();
        long pageSize = query.getPageSize();
        String keywords = query.getKeywords();
        String typeCode = query.getTypeCode();

        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(keywords), SysDictItem::getName, keywords)
                .eq(StringUtils.isNotBlank(typeCode), SysDictItem::getTypeCode, typeCode)
                .select(SysDictItem::getId, SysDictItem::getName, SysDictItem::getValue, SysDictItem::getStatus)
                .orderByAsc(SysDictItem::getSort)
                .orderByDesc(SysDictItem::getCreateTime);
        // 查询数据
        Page<SysDictItem> page = this.page(new Page<>(pageNum, pageSize), queryWrapper);

        // 实体转换
        List<SysDictItemVo> list = dictItemConvert.convertVoList(page.getRecords());
        PageVo<SysDictItemVo> pageVo = new PageVo<>();
        pageVo.setPages((int) page.getPages());
        pageVo.setTotal(page.getTotal());
        pageVo.setList(list);
        return pageVo;
    }

    /**
     * 字典数据项表单详情
     *
     * @param param 字典数据项ID
     * @return
     */
    public SysDictItemVo detail(IdParam param) {
        LambdaQueryWrapper<SysDictItem> queryWrapper = new LambdaQueryWrapper<SysDictItem>()
                .eq(SysDictItem::getId, param.getId())
                .select(SysDictItem::getId,
                        SysDictItem::getTypeCode,
                        SysDictItem::getName,
                        SysDictItem::getValue,
                        SysDictItem::getStatus,
                        SysDictItem::getSort,
                        SysDictItem::getRemark);
        // 获取entity
        SysDictItem entity = this.getOne(queryWrapper);
        Assert.isTrue(entity != null, "字典数据项不存在");

        // 实体转换
        SysDictItemVo dictItemVo = dictItemConvert.convertVo(entity);
        return dictItemVo;
    }

    /**
     * 新增字典数据项
     *
     * @param dictItemParam 字典数据项表单
     * @return
     */
    public boolean create(SysDictItemParam dictItemParam) {
        // 实体对象转换 form->entity
        SysDictItem entity = dictItemConvert.convertPo(dictItemParam);
        // 持久化
        boolean result = this.save(entity);
        return result;
    }

    /**
     * 修改字典数据项
     *
     * @param id           字典数据项ID
     * @param dictItemParam 字典数据项表单
     * @return
     */
    public boolean update(Long id, SysDictItemParam dictItemParam) {
        SysDictItem entity = this.getById(id);
        Assert.isTrue(entity != null, "字典数据项不存在");
        SysDictItem dictItem = dictItemConvert.convertPo(dictItemParam);
        boolean result = this.updateById(dictItem);
        return result;
    }

    /**
     * 删除字典数据项
     *
     * @param param 字典数据项ID集合
     * @return
     */
    public boolean deleteBatch(IdsParam param) {
        // 删除字典数据项
        boolean result = this.removeByIds(param.getIds());
        return result;
    }

}




